#ifndef SPDLOG_H
#define SPDLOG_H

#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
#include <sys/syscall.h>
#include <errno.h>

//spdlog

#define gettid() syscall(SYS_gettid)
#define __FILENAME__ (strrchr(__FILE__, '/') ? (strrchr(__FILE__, '/') + 1):__FILE__)

#define LOGV(format, ...) (spdLog.v("[%s:%d:%s] " format "", __FILENAME__ , __LINE__, __FUNCTION__, ##__VA_ARGS__))
#define LOGD(format, ...) (spdLog.d("[%s:%d:%s] " format "", __FILENAME__ , __LINE__, __FUNCTION__, ##__VA_ARGS__))
#define LOGI(format, ...) (spdLog.i("[%s:%d:%s] " format "", __FILENAME__ , __LINE__, __FUNCTION__, ##__VA_ARGS__))
#define LOGW(format, ...) (spdLog.w("[%s:%d:%s] " format "", __FILENAME__ , __LINE__, __FUNCTION__, ##__VA_ARGS__))
#define LOGE(format, ...) (spdLog.e("[%s:%d:%s] " format "", __FILENAME__ , __LINE__, __FUNCTION__, ##__VA_ARGS__))

typedef enum {
	SPDLOG_LEVEL_TRACE = 0,
	SPDLOG_LEVEL_DEBUG,
	SPDLOG_LEVEL_INFO,
	SPDLOG_LEVEL_WARN,
	SPDLOG_LEVEL_ERROR,
	SPDLOG_LEVEL_OFF = 6,
}SPDLOG_LEVEL;

typedef enum {
	SPDLOG_MODE_STDOUT = 0x01,
	SPDLOG_MODE_BASICFILE = 0x02,
	SPDLOG_MODE_ROTAFILE = 0x04,
}SPDLOG_MODE;

class SpdLog
{
public:
	SpdLog();
	void init(int mode = SPDLOG_MODE_STDOUT, const char *logger_name = "spdlog", 
		const char *filename = NULL, int max_file_size = 1024*1024*5, int max_files = 3);
	void setLevel(int level);
	void loadEnvLevel();
	void v(const char *format, ...);
	void d(const char *format, ...);
	void i(const char *format, ...);
	void w(const char *format, ...);
	void e(const char *format, ...);
};

extern SpdLog spdLog;

#endif